package com.ruoyi.match.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.match.domain.LeaderTeam;
import com.ruoyi.match.domain.vo.TeamsAddVo;
import com.ruoyi.match.domain.vo.TeamsVo;
import com.ruoyi.match.service.ILeaderTeamService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.match.domain.Teams;
import com.ruoyi.match.service.ITeamsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 队伍Controller
 * 
 * @author zhangrui
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/match/teams")
public class TeamsController extends BaseController
{
    @Autowired
    private ITeamsService teamsService;

    @Autowired
    private ILeaderTeamService leaderTeamService;

    /**
     * 查询队伍列表
     */
    @PreAuthorize("@ss.hasPermi('match:teams:list')")
    @GetMapping("/list")
    public TableDataInfo list(Teams teams)
    {
        startPage();
        List<TeamsVo> volist = teamsService.selectTeamsVoList(teams);
        return getDataTable(volist);
    }




    /**
     * 导出队伍列表
     */
    @PreAuthorize("@ss.hasPermi('match:teams:export')")
    @Log(title = "队伍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Teams teams)
    {
        List<Teams> list = teamsService.selectTeamsList(teams);
        ExcelUtil<Teams> util = new ExcelUtil<Teams>(Teams.class);
        util.exportExcel(response, list, "队伍数据");
    }

//    /**
//     * 获取队伍详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('match:teams:query')")
//    @GetMapping(value = "/{teamId}")
//    public AjaxResult getInfo(@PathVariable("teamId") Long teamId)
//    {
//        return success(teamsService.selectTeamsByTeamId(teamId));
//    }


    /**
     * 获取队伍详细信息(Vo)
     */
    @GetMapping(value = "/{teamId}")
    public AjaxResult getInfoVo(@PathVariable("teamId") Long teamId)
    {
        return success(teamsService.selectTeamsInfoVoList(teamId));
    }


    /**
     * 根据活动id获取队伍详细信息(Vo)
     */
    @GetMapping(value = "/byActId/{actId}")
    public TableDataInfo getTeamsInfoByActId(@PathVariable("actId") Long actId)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<TeamsVo> volist = teamsService.selectTeamsVoListByActId(actId, userId);
        return getDataTable(volist);
    }



//    /**
//     * 新增队伍
//     */
////    @PreAuthorize("@ss.hasPermi('match:teams:add')")
//    @Log(title = "队伍", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Teams teams)
//    {
//        return toAjax(teamsService.insertTeams(teams));
//    }


    /**
     * 活动页面新增队伍
     */
//    @PreAuthorize("@ss.hasPermi('match:teams:add')")
    @Log(title = "队伍", businessType = BusinessType.INSERT)
    @PostMapping("/addTeam")
    public AjaxResult addTeamWithActId(@RequestBody Teams teams)
    {
        Long userId = SecurityUtils.getUserId();
        TeamsAddVo teams2 = new TeamsAddVo();

        teams2.setActId(teams.getActId());
        teams2.setTeamName(teams.getTeamName());
        teams2.setTeamOutline(teams.getTeamOutline());

        teams2.setLeaderId(userId);
        teams2.setTeamStatus(teams.getTeamStatus());


        return toAjax(teamsService.addTeams(teams2));
    }

    /**
     * 修改队伍
     */
//    @PreAuthorize("@ss.hasPermi('match:teams:edit')")
    @Log(title = "队伍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Teams teams)
    {
        return toAjax(teamsService.updateTeams(teams));
    }

    /**
     * 删除队伍
     */
//    @PreAuthorize("@ss.hasPermi('match:teams:remove')")
    @Log(title = "队伍", businessType = BusinessType.DELETE)
	@DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds)
    {
        return toAjax(teamsService.deleteTeamsByTeamIds(teamIds));
    }

    /**
     * 查询我加入的队伍列表
     */
//    @PreAuthorize("@ss.hasPermi('match:teams:list')")
    @GetMapping("/joinedTeams/list")
    public TableDataInfo joinedTeams()
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<TeamsVo> volist = teamsService.selectJoinedTeamsVoList(userId);
        if (volist == null){
            return null;
        }
        return getDataTable(volist);

    }

    /**
     * 查询我领导的队伍列表
     */
    @GetMapping("/myTeams/list")
    public TableDataInfo showMyTeams()
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<TeamsVo> volist = teamsService.showMyTeamList(userId);
        if (volist == null){
            return null;
        }
        return getDataTable(volist);

    }

    /**
     * 解散队伍
     * @param
     * @return
     */
    @DeleteMapping("/myTeams/delete/{teamId}")
    public AjaxResult deleteTeam(@PathVariable("teamId") Long teamIds)
    {
        return toAjax(teamsService.deleteTeam(teamIds));
    }
}
